<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="环境名称" prop="configName">
        <el-input
            v-model="queryParams.configName"
            placeholder="请输入环境名称"
            clearable
            style="width: 240px"
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['encryption:envconfig:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['encryption:envconfig:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['encryption:envconfig:remove']"
        >删除
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" ref="envRef"
              @selection-change="handleSelectionChange"
              :data="envList">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="环境名称" align="center" prop="configName"/>
      <el-table-column label="加密算法" align="center" prop="encryptType"/>
      <el-table-column label="请求公钥" align="center" prop="requestPublicKey"/>
      <el-table-column label="请求私钥" align="center" prop="requestPrivateKey"/>
      <el-table-column label="响应公钥" align="center" prop="responsePublicKey"/>
      <el-table-column label="响应私钥" align="center" prop="responsePrivateKey"/>
      <el-table-column label="创建时间" align="center" prop="createdTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createdTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="160" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['encryption:envconfig:edit']">修改
          </el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['encryption:envconfig:remove']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="envconfigRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="环境名称" prop="configName">
          <el-input v-model="form.configName" placeholder="请输入环境名称"/>
        </el-form-item>
        <el-form-item label="加密算法" prop="encryptType">
          <el-input v-model="form.encryptType" placeholder="请输入加密算法"/>
        </el-form-item>
        <el-form-item label="请求公钥" prop="requestPublicKey">
          <el-input v-model="form.requestPublicKey" type="textarea" placeholder="请输入请求公钥"/>
        </el-form-item>
        <el-form-item label="请求私钥" prop="requestPrivateKey">
          <el-input v-model="form.requestPrivateKey" type="textarea" placeholder="请输入请求私钥"/>
        </el-form-item>
        <el-form-item label="响应公钥" prop="responsePublicKey">
          <el-input v-model="form.responsePublicKey" type="textarea" placeholder="请输入响应公钥"/>
        </el-form-item>
        <el-form-item label="响应私钥" prop="responsePrivateKey">
          <el-input v-model="form.responsePrivateKey" type="textarea" placeholder="请输入响应私钥"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>

</template>
<script setup>
import {ref} from "vue";
import {addEnvConfig, getConfigList, delEnvConfig, updateEnvConfig, getConfig} from "@/api/encryption/envconfig.js";

const {proxy} = getCurrentInstance();
const envList = ref([]);
const open = ref(false);
const loading = ref(false);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const dateRange = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    configName: undefined
  },
  rules: {
    configName: [{required: true, message: "环境名称不能为空", trigger: "blur"}],
    requestPublicKey: [{required: true, message: "请求公钥不能为空", trigger: "blur"}],
    requestPrivateKey: [{required: true, message: "请求私钥不能为空", trigger: "blur"}],
    responsePublicKey: [{required: true, message: "响应公钥不能为空", trigger: "blur"}],
    responsePrivateKey: [{required: true, message: "响应私钥不能为空", trigger: "blur"}],
  },
});

const {queryParams, form, rules} = toRefs(data);

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  // getList();
};

/** 选择条数  */
const handleSelectionChange = (val) => {
  console.log(val);
  ids.value = val.map(item => item.configId);
  single.value = val.length !== 1;
  multiple.value = !val.length;
};

/** 添加按钮操作 */
const handleAdd = () => {
  title.value = "添加环境配置";
  open.value = true;
  proxy.resetForm("form");
};
/** 修改按钮操作 */
const handleUpdate = (row) => {
  title.value = "修改环境配置";
  open.value = true;
  const configId = row.configId || ids.value[0];
  console.log('configId', configId);
  getConfig(configId).then(response => {
    form.value = response.data;
  });
};

/** 删除按钮操作 */
const handleDelete = (row) => {
  const configId = row.configId || ids.value;
  proxy.$confirm("确定删除选中的环境配置吗?", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning"
  }).then(() => {
    delEnvConfig(configId).then(response => {
      if (response.code === 200) {
        proxy.$message.success("删除成功");
        getList();
      } else {
        proxy.$message.error(response.message);
      }
    });
  });
};


/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

/**添加修改表单重置 */
const reset = () => {
  form.value = {
    configId: undefined,
    configName: undefined,
    encryptType: undefined,
    requestPublicKey: undefined,
    requestPrivateKey: undefined,
    responsePublicKey: undefined,
    responsePrivateKey: undefined
  };
  proxy.resetForm("envconfigRef");
}

const submitForm = () => {
  proxy.$refs["envconfigRef"].validate(async valid => {
    if (valid) {
      if (form.value.configId) {
        let updateResponse = await updateEnvConfig(form.value);
        if (updateResponse.code === 200) {
          proxy.$message.success("修改成功");
        } else {
          proxy.$message.error(updateResponse.message)
        }
      } else {
        let addResponse = await addEnvConfig(form.value);
        if (addResponse.code === 200) {
          proxy.$message.success("添加成功");
        } else {
          proxy.$message.error(addResponse.message)
        }
      }
      open.value = false;
      reset();
      getList();
    }
  });
}

const cancel = () => {
  open.value = false;
  reset();
}

/** 查询配置列表 */
function getList() {
  loading.value = true;
  getConfigList().then(response => {
    envList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  }).catch(() => {
    loading.value = false;
  });
}

getList();
</script>
